package net.hubt.itc.ui;

import net.hubt.itc.data.LookupCommand;
import net.hubt.itc.io.net.HttpUtils;
import net.hubt.itc.ui.utils.CTable;
import net.hubt.itc.util.LayoutUtils;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class CardSwipeActivity extends BaseActivity {

	TextView tvStatus;
	LinearLayout mainLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initMainLayoutParams();
	}

	@Override
	protected void onStart() {
		super.onStart();
		executeGetCardStatusTask();
	}

	private void executeGetCardStatusTask() {
		prepareParamsForTask();
		new GetCardStatusTask().execute(baseUrl + "/quetthe.php", "msv",
				userId, "type", LookupCommand.CMD_GET_CARD_STATUS + "");
	}

	// performed when the activity is loaded
	class GetCardStatusTask extends AsyncTask<String, String, String> {
		ProgressDialog dlgProgress;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dlgProgress = ProgressDialog
					.show(CardSwipeActivity.this,
							getString(R.string.ui_card_swipe_state_get_card_status_short_msg),
							getString(R.string.ui_card_swipe_state_get_card_status_msg),
							true);
		}

		@Override
		protected String doInBackground(String... addresses) {
			try {
				String serverString = HttpUtils.urlContentPost(addresses);
				return serverString;
			} catch (Exception ex) {
				// nen tra ve 1 string la 1 so , vd : return "-1";
				// sau do xu ly so "-1" nay va hien thong bao loi
				// trong ham onPostExecute
				return "-1";

			}
		}

		@Override
		protected void onProgressUpdate(String... values) {
			// Log.e("onProgressUpdate", "onProgressUpdate");
		}

		@Override
		protected void onPostExecute(String serverString) {
			processResult(serverString);
			dlgProgress.dismiss();
		}
	}

	class DoCheckCardStatusTask extends AsyncTask<String, Integer, String> {
		ProgressDialog dlgProgress;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dlgProgress = ProgressDialog
					.show(CardSwipeActivity.this,
							getString(R.string.ui_card_swipe_state_check_card_status_short_msg),
							getString(R.string.ui_card_swipe_state_check_card_status_msg),
							true);
		}

		@Override
		protected String doInBackground(String... addresses) {
			try {
				String serverString = HttpUtils.urlContentPost(addresses);

				return serverString;
			} catch (Exception ex) {
				return ex.getMessage();
			}
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// Log.e("onProgressUpdate", "onProgressUpdate");
		}

		@Override
		protected void onPostExecute(String serverString) {
			processResult(serverString);
			dlgProgress.dismiss();
		}
	}

	protected void initMainLayoutParams() {
		mainLayout = new LinearLayout(this);
		LayoutParams lparams = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		lparams.setMargins(0, 0, 0, 3);
		mainLayout.setLayoutParams(lparams);
		mainLayout.setOrientation(LinearLayout.VERTICAL);
		setContentView(mainLayout);
	}

	protected void processResult(String strResult) {
		Log.e("RESULT", strResult);
		int result = Integer.parseInt(strResult);
		switch (result) {
		case LookupCommand.RESULT_UNCHECKED:
			buildCheckCardRequireUI();
			break;
		case LookupCommand.RESULT_CHECKED:
			displayNotification();
			break;
		default:
			displayErrorDialog();
			break;
		}
	}

	protected void displayErrorDialog() {
		AlertDialog dialog = new AlertDialog.Builder(this).create();
		dialog.setTitle(getString(R.string.ui_card_swipe_state_error_title));
		// dialog.setMessage(getString(R.string.ui_trans_error_msg));
		dialog.setMessage(getString(R.string.ui_card_swipe_state_error_msg));
		dialog.setIcon(android.R.drawable.ic_dialog_info);
		dialog.setButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	protected void displayNotification() {
		AlertDialog dialog = new AlertDialog.Builder(this).create();
		dialog.setTitle(getString(R.string.ui_card_swipe_desc_short));
		// dialog.setMessage(getString(R.string.ui_trans_error_msg));
		dialog.setMessage(getString(R.string.ui_card_swipe_state_success));
		dialog.setIcon(android.R.drawable.ic_dialog_info);
		dialog.setButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();

			}
		});
		dialog.show();
		mainLayout.removeAllViews();
		tvStatus = makeImageTextView(
				CardSwipeActivity.this,
				getResources().getDrawable(R.drawable.check_64),
				getResources().getDrawable(
						R.drawable.green_theme_normal_textview), getResources()
						.getString(R.string.ui_card_swipe_state_ok));
		mainLayout.addView(tvStatus);

	}

	protected void buildCheckCardRequireUI() {
		Button btnCheckCard;
		tvStatus = makeImageTextView(
				this,
				getResources().getDrawable(R.drawable.checklist_64),
				getResources().getDrawable(
						R.drawable.orange_theme_normal_textview),
				getResources().getString(
						R.string.ui_card_swipe_state_need_swipe));
		mainLayout.addView(tvStatus);
		mainLayout.addView(btnCheckCard = makeImageButton(
				this,
				getResources().getDrawable(R.drawable.check_64),
				getResources().getDrawable(R.drawable.blue_theme_textview),
				getResources().getString(
						R.string.ui_card_swipe_state_check_card_msg)));
		btnCheckCard.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				new DoCheckCardStatusTask().execute(baseUrl + "/quetthe.php",
						"msv", userId, "type",
						LookupCommand.CMD_REQUIRE_TO_CHECK_CARD + "");

			}
		});
	}

	public static TextView makeImageTextView(Context ctx, Drawable img,
			Drawable background, String text) {
		TextView tv = new TextView(ctx);
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		// create space between 2 adjacent items
		lp.setMargins(0, 0, 0, 3);
		tv.setLayoutParams(lp);
		// draw image on the left
		tv.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
		tv.setGravity(Gravity.CENTER_VERTICAL);
		tv.setBackgroundDrawable(background);
		tv.setTextColor(CTable.DEFAULT_TEXT_COLOR);
		tv.setText(text);
		tv.setShadowLayer(.5f, 1, 1, CTable.DEFAULT_TEXT_SHADOW_COLOR);
		return tv;
	}

	public static Button makeImageButton(Context ctx, Drawable img,
			Drawable background, String text) {
		Button btnCheck = new Button(ctx);
		btnCheck.setLayoutParams(LayoutUtils.makeLinearLayoutParamFillWidth());
		// draw image on the left
		btnCheck.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
		btnCheck.setGravity(Gravity.CENTER_VERTICAL);
		btnCheck.setBackgroundDrawable(background);
		btnCheck.setTextColor(CTable.DEFAULT_TEXT_COLOR);
		btnCheck.setText(text);
		btnCheck.setShadowLayer(.5f, 1, 1, CTable.DEFAULT_TEXT_SHADOW_COLOR);
		return btnCheck;
	}

}
